#include "func.h"

int main(int argc,char *argv[])
{
    ARGS_CHECK(argc,3);
    int client_fd=socket(AF_INET,SOCK_DGRAM,0);
    ERROR_CHECK(client_fd,-1,"socket");
    int ret;
    struct sockaddr_in serAddr;
    serAddr.sin_family=AF_INET;
    serAddr.sin_addr.s_addr=inet_addr(argv[1]);
    serAddr.sin_port=htons(atoi(argv[2]));
    ret=bind(client_fd,(struct sockaddr *)&serAddr,sizeof(serAddr));

    char buf[1000];
    fd_set rdset;

    while(1)
    {
        FD_ZERO(&rdset);
        FD_SET(STDIN_FILENO,&rdset);
        FD_SET(client_fd,&rdset);
        select(client_fd+1,&rdset,NULL,NULL,NULL);
        if(FD_ISSET(STDIN_FILENO,&rdset))
        {
            memset(buf,0,sizeof(buf));
            read(STDIN_FILENO,&buf,sizeof(buf));
            sendto(client_fd,buf,strlen(buf)-1,0,(struct sockaddr*)&serAddr,sizeof(serAddr));
        }
        if(FD_ISSET(client_fd,&rdset))
        {
            memset(buf,0,sizeof(buf));
            recvfrom(client_fd,buf,sizeof(buf),0,NULL,NULL);
            printf("%s\n",buf);
        }
    }
    return 0;
}